package com.safe.ocs.services;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.TimerTask;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.safe.ocs.beans.ST_Person;
import com.safe.ocs.beans.Vocation;
import com.safe.ocs.beans.LaterMSG;

import com.safe.ocs.utils.HibernateUtil;
/**
 * 
 * @author 李楠君
 * 解析信息到txt文本中
 */
public class readVocation extends TimerTask{
	int laghtime=0;
	int lagmtime=0;
	LaterMSG lMsg=null;
	ST_Person sPerson=null;
	@Override
	public void run(){
		 File file=new File("d:/show.txt");
		 FileWriter fWriter;  //输出流
		 BufferedWriter bw;//缓存
		try {
			 fWriter = new FileWriter(file,false);
			 bw= new BufferedWriter(fWriter);
			 List<Vocation> lstLeave=this.leaveMSG();//请假
			 List<LaterMSG> listLate=this.lateMSG();//迟到
			 List<ST_Person> absence=this.absenceMSG();//缺勤
	         //请假信息
			 if(lstLeave.size()>0){
			 for(int i=0;i<lstLeave.size();i++){
			  if(lstLeave.get(i).getReason()==null){
				 bw.write("\t\t"+lstLeave.get(i).getName()+"\t\t"+"请假"+"\t\t"+" "+"\r\n");
				}
			    else{
					 bw.write("\t\t"+lstLeave.get(i).getName()+"\t\t"+"请假"+"\t\t"+lstLeave.get(i).getReason()+"\r\n");
				 }
			   }
			 }
		     //迟到人员信息
			 if(listLate.size()>0){
			 for(int i=0;i<listLate.size();i++){
				 int hstime=Integer.parseInt(listLate.get(i).getSect_InTime().substring(0, 2).trim());//获取规定上班小时
				 int mstime=Integer.parseInt(listLate.get(i).getSect_InTime().substring(3, 5).trim());//获取规定上班分钟
				 int hDtime=Integer.parseInt(listLate.get(i).getBrush_Time().substring(0,2).trim());//获取上班打卡小时
				 int mDtime=Integer.parseInt(listLate.get(i).getBrush_Time().substring(3,5).trim());//获取上班打卡分钟
				 if(hDtime>hstime)
				 {
					 if(mDtime<mstime)
					 {
						 if(hDtime-hstime==1)
						 {
							 laghtime=hDtime-hstime-1;
							 lagmtime=(60-mstime+mDtime);
							 bw.write("\t\t"+listLate.get(i).getPerson_Name()+"\t\t"+"迟到"+"\t\t"+laghtime+"小时"+lagmtime+"分钟"+"\r\n");
						 }
						 else 
						 {
							 laghtime=hDtime-hstime-1;//小时差
							 lagmtime=60-mstime+mDtime;//分钟差
							 bw.write("\t\t"+listLate.get(i).getPerson_Name()+"\t\t"+"迟到"+"\t\t"+laghtime+"小时"+lagmtime+"分钟"+"\r\n");
						 }
					 }
					 else 
					 {
						laghtime=hDtime-hstime;
						lagmtime=mDtime-mstime;
						bw.write("\t\t"+listLate.get(i).getPerson_Name()+"\t\t"+"迟到"+"\t\t"+laghtime+"小时"+lagmtime+"分钟"+"\r\n");
					 }
					 
				 }
				 else if(hDtime==hstime&&mDtime>mstime)
				 {
					 laghtime=hDtime-hstime;
					 lagmtime=mDtime-mstime;
					 bw.write("\t\t"+listLate.get(i).getPerson_Name()+"\t\t"+"迟到"+"\t\t"+laghtime+"小时"+lagmtime+"分钟"+"\r\n");
				 }
			}
		 }
		 //缺勤信息
		 if(absence.size()>0){
			for(int i=0;i<absence.size();i++){
		        bw.write("\t\t"+absence.get(i).getPerson_Name()+"\t\t"+"缺勤"+"\t\t"+" "+"\r\n");
			}
		 }
		 bw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 缺勤信息的方法
	 * @return
	 */

	private List<ST_Person> absenceMSG(){
		Session session=HibernateUtil.getSession();
		Transaction transaction=session.beginTransaction();
		List<ST_Person> lMsgs=new ArrayList<ST_Person>();
		List list=new ArrayList();
		String findAbsenteeism="select Person_Name from ST_Person where Person_ID in(select Person_ID from KQ_Person where Person_ID not in(select Person_ID from vocation union select Person_ID from LaterMSG where Brush_Date=(select convert(varchar(10),getdate(),120))))";
		try 
		{
			list=session.createSQLQuery(findAbsenteeism).list();
			String[] laterArray=new String[list.size()];
			if(list.size()>0){
				for(int i=0;i<list.size();i++){
					sPerson=new ST_Person();
					laterArray[i]=list.get(i).toString();
					sPerson.setPerson_Name(laterArray[i]);
					lMsgs.add(sPerson);
			}
		}
			transaction.commit();
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
		}finally{
			session.close();
		}
		return lMsgs;
	}
	
	/**
	 * 迟到信息的方法
	 * @return
	 */
	private List<LaterMSG> lateMSG(){
		Session session=HibernateUtil.getSession();
		Transaction transaction=session.beginTransaction();
		List list = new ArrayList();
		List<LaterMSG> laterMSGs=new ArrayList<LaterMSG>();
		String findlate="select Person_Name,Sect_InTime,Brush_Time from LaterMSG as lmsg where lmsg.Brush_Date=(select convert(varchar(10),getdate(),120))";
		try {
		    list=session.createSQLQuery(findlate).list();
			if(list.size()>0){
				for(int i=0;i<list.size();i++){
					lMsg=new LaterMSG();
					Object[] laterArray = (Object[])list.get(i);
					String strname=(String) laterArray[0];
					String strInTime=(String) laterArray[1];
					String strOutTime=(String) laterArray[2];
					lMsg.setPerson_Name(strname);
					lMsg.setSect_InTime(strInTime);
					lMsg.setBrush_Time(strOutTime);
					laterMSGs.add(lMsg);
				}
			}
			transaction.commit();
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
		}finally{
			session.close();
		}
		return laterMSGs;
	}
	/**
	 * 请假信息的方法
	 * @return
	 */
	private List<Vocation> leaveMSG(){
		Session session=HibernateUtil.getSession();
		Transaction transaction=session.beginTransaction();
		List<Vocation> liVocations=new ArrayList<Vocation>();
		List list = new ArrayList();
		Vocation vocation=null;
		String findVocation="select name,reason from vocation";
		try {
			//读取请假的LIST
			list=session.createSQLQuery(findVocation).list();
			if(list.size()>0){
				for(int i=0;i<list.size();i++){
					vocation=new Vocation();
					Object[] laterArray = (Object[])list.get(i);
					String strname=(String) laterArray[0];
					String strreason=(String) laterArray[1];
					vocation.setName(strname);
					vocation.setReason(strreason);
					liVocations.add(vocation);
				}
		   }
			transaction.commit();
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
		}finally{
			session.close();
		}
		return liVocations;
	}
}
